package com.zzrc.grape.controller;

import com.zzrc.grape.model.order.ChangeOrderStatusReq;
import com.zzrc.grape.model.order.OrderSelectReq;
import com.zzrc.grape.service.OrderService;
import com.zzrc.grape.util.BasePageData;
import com.zzrc.grape.util.WebResponseCode;
import com.zzrc.peony.model.db.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    OrderService orderService;

    /**
     * 去订单状态首页
     * @param request
     * @param response
     * @return modelView
     */
    @RequestMapping("goOrderStatusIndex")
    public ModelAndView goOrderStatusIndex(HttpServletRequest request, HttpServletResponse response){
        ModelAndView modelView = new ModelAndView();
        modelView.setViewName("order/orderStatusIndex");
        return modelView;
    }

    /**
     * 去订单查询首页
     * @param request
     * @param response
     * @return modelView
     */
    @RequestMapping("goOrderSelectIndex")
    public ModelAndView goOrderSelectIndex(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView modelView = new ModelAndView();
        modelView.setViewName("order/orderSelectIndex");
        return modelView;
    }

    /**
     * 修改订单状态
     * @param changeOrderStatusReq 修改订单状态请求体
     * @return basePageData
     */
    @PostMapping(value = "changeOrderStatus", consumes = "application/json;charset=UTF-8")
    @ResponseBody
    public BasePageData changeOrderStatus(@RequestBody ChangeOrderStatusReq changeOrderStatusReq) {
        BasePageData basePageData = new BasePageData();
        String env = changeOrderStatusReq.getEnv();
        String loanGid = changeOrderStatusReq.getLoanGid();
        String operation = changeOrderStatusReq.getOperation();
        System.out.println(loanGid + " "+ env + " "+ operation);
        Boolean result = orderService.changeOrderStatus(loanGid, env, operation);
        if (result) {
            basePageData.setCode(WebResponseCode.SUCCESS);
            basePageData.setMsg(WebResponseCode.SUCCESSINFO);
        } else {
            basePageData.setCode(WebResponseCode.FAIL);
            basePageData.setMsg(WebResponseCode.FAILINFO);
        }
        return basePageData;
    }

    /**
     * 借款列表查询
     * @param orderSelectReq 借款列表查询请求体
     * @return basePageData
     */
    @RequestMapping(value = "getRecordLoanList", consumes = "application/json;charset=UTF-8")
    @ResponseBody
    public BasePageData getRecordLoanList(@RequestBody OrderSelectReq orderSelectReq, HttpServletRequest request,
                                    HttpServletResponse response) {
        BasePageData basePageData = new BasePageData();
        String loanGid = orderSelectReq.getLoanGid();
        List<RecordLoan> recordLoans = orderService.getRecordLoanList(loanGid);
        basePageData.setCode(WebResponseCode.SUCCESS);
        basePageData.setMsg(WebResponseCode.SUCCESSINFO);
        basePageData.setCount(recordLoans.size());
        basePageData.setData(recordLoans);
        return basePageData;

    }

    /**
     * 还款列表查询
     * @param orderSelectReq 还款列表查询请求体
     * @return basePageData
     */
    @RequestMapping(value = "getRecordRepaymentList", consumes = "application/json;charset=UTF-8")
    @ResponseBody
    public BasePageData getRecordRepaymentList(@RequestBody OrderSelectReq orderSelectReq, HttpServletRequest request,
                                    HttpServletResponse response) {
        BasePageData basePageData = new BasePageData();
        String loanGid = orderSelectReq.getLoanGid();
        List<RecordRepayment> recordRepayments = orderService.getRecordRepaymentList(loanGid);
        basePageData.setCode(WebResponseCode.SUCCESS);
        basePageData.setMsg(WebResponseCode.SUCCESSINFO);
        basePageData.setCount(recordRepayments.size());
        basePageData.setData(recordRepayments);
        return basePageData;

    }

    /**
     * 还款计划列表查询
     * @param orderSelectReq 还款列表查询请求体
     * @return basePageData
     */
    @RequestMapping(value = "getRecordRepaymentPlanList", consumes = "application/json;charset=UTF-8")
    @ResponseBody
    public BasePageData getRecordRepaymentPlanList(@RequestBody OrderSelectReq orderSelectReq, HttpServletRequest request,
                                               HttpServletResponse response) {
        BasePageData basePageData = new BasePageData();
        String loanGid = orderSelectReq.getLoanGid();
        List<RecordRepaymentPlan> recordRepaymentPlans = orderService.getRecordRepaymentPlanList(loanGid);
        basePageData.setCode(WebResponseCode.SUCCESS);
        basePageData.setMsg(WebResponseCode.SUCCESSINFO);
        basePageData.setCount(recordRepaymentPlans.size());
        basePageData.setData(recordRepaymentPlans);
        return basePageData;

    }

    /**
     * 打款记录列表查询
     * @param orderSelectReq 打款记录列表查询请求体
     * @return basePageData
     */
    @RequestMapping(value = "getTransWithdrawList", consumes = "application/json;charset=UTF-8")
    @ResponseBody
    public BasePageData getTransWithdrawList(@RequestBody OrderSelectReq orderSelectReq, HttpServletRequest request,
                                                   HttpServletResponse response) {
        BasePageData basePageData = new BasePageData();
        String loanGid = orderSelectReq.getLoanGid();
        List<TransWithdraw> transWithdraws = orderService.getTransWithdrawList(loanGid);
        basePageData.setCode(WebResponseCode.SUCCESS);
        basePageData.setMsg(WebResponseCode.SUCCESSINFO);
        basePageData.setCount(transWithdraws.size());
        basePageData.setData(transWithdraws);
        return basePageData;

    }

    /**
     * 还款记录列表查询
     * @param orderSelectReq 还款记录列表查询请求体
     * @return basePageData
     */
    @RequestMapping(value = "getTransDeductionList", consumes = "application/json;charset=UTF-8")
    @ResponseBody
    public BasePageData getTransDeductionList(@RequestBody OrderSelectReq orderSelectReq, HttpServletRequest request,
                                             HttpServletResponse response) {
        BasePageData basePageData = new BasePageData();
        String loanGid = orderSelectReq.getLoanGid();
        List<TransDeduction> transDeductions = orderService.getTransDeductionList(loanGid);
        basePageData.setCode(WebResponseCode.SUCCESS);
        basePageData.setMsg(WebResponseCode.SUCCESSINFO);
        basePageData.setCount(transDeductions.size());
        basePageData.setData(transDeductions);
        return basePageData;

    }
}
